package org.hishenframework.core.web.menu;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.web.bind.annotation.RequestMapping;

//http://blog.csdn.net/inthirties/archive/2009/08/28/4493961.aspx
//http://www.fengfly.com/plus/view-191037-1.html
public class MenuItemAnnotationBeanProcessor implements BeanPostProcessor, PriorityOrdered {

	/**
	 * 初始化bean后
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		Class<?> clazz = bean.getClass();
		MenuItem menuItem = clazz.getAnnotation(MenuItem.class);
		if (menuItem != null) {
			//获取请求路径
			String requestMapping = menuItem.requestMapping();
			if (requestMapping.length() == 0) {
				RequestMapping requestMappingAnn = clazz.getAnnotation(RequestMapping.class);
				requestMapping = requestMappingAnn.value()[0];
			}
			MenuContext.addMenuItems(menuItem.type(), menuItem.label(), requestMapping, menuItem.order());
		}
		return bean;
	}

	/**
	 * 初始化bean前
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		return bean;
	}

	@Override
	public int getOrder() {
		return Ordered.LOWEST_PRECEDENCE;
	}

}
